<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :before-close="cancel" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
      <el-form-item label="购买次数" prop="times">
        <el-input type="number" v-model="form.times" style="width: 300px;" ref="times"/>
      </el-form-item>
      <el-form-item label="金额(元)" prop="price">
        <el-input type="number" v-model="form.price" style="width: 300px;" ref="price"/>
      </el-form-item>
      <el-form-item label="赠送次数">
        <el-input type="number" v-model="form.give_times" style="width: 300px;" ref="giveTimes"/>
      </el-form-item>
      <el-form-item label="排序">
        <el-input type="number" v-model="form.sort" style="width: 300px;" />
      </el-form-item>
      <el-form-item label="是否显示">
        <el-radio v-model="form.status" :label="1">是</el-radio>
        <el-radio v-model="form.status" :label="0" style="width: 200px;">否</el-radio>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <!--<el-input v-model="form.groupName" />-->
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { add, edit } from '@/api/yxSystemGroupData'
import picUpload from '@/components/pic-upload'
export default {
  components: { picUpload },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      loading: false, dialog: false,
      form: {
        id: '',
        groupName: 'yshop_recharge_price_ways',
        price: 0,
        give_times: 0,
        sort: 0,
        status: 1
      },
      rules: {
          times: [
              { required: true, message: '请输入购买次数', trigger: 'blur' },
              { min: 2, max: 20, message: '长度在 1 到 40 个字符', trigger: 'blur' }
          ], 
          price: [
              { required: true, message: '请输入金额', trigger: 'blur' }
          ]
      } 
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      var times = this.$refs.times.value
      if (times == 0 || times == null) {
        alert('购买次数不能为空或者为0')
        this.loading = false
        return false
      }

      var price = this.$refs.price.value
      if (price == 0 || times == null) {
        alert('金额不能为空或者为0')
        this.loading = false
        return false
      }

      var giveTimes = this.$refs.giveTimes.value
      if (giveTimes == null) {
        alert('赠送次数不能为空')
        this.loading = false
        return false
      }

      add(this.form).then(res => {
          this.resetForm()
          this.$notify({
            title: '添加成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.$parent.init()
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
    },
    doEdit() {
      var times = this.$refs.times.value
      if (times == 0 || times == null) {
        alert('购买次数不能为空或者为0')
        this.loading = false
        return false
      }

      var price = this.$refs.price.value
      if (price == 0 || times == null) {
        alert('金额不能为空或者为0')
        this.loading = false
        return false
      }

      var giveTimes = this.$refs.giveTimes.value
      if (giveTimes == null) {
        alert('赠送次数不能为空')
        this.loading = false
        return false
      }

      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
      
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        groupName: 'yshop_recharge_price_ways',
        price: 0,
        give_times: 0,
        sort: 0,
        status: 1
      }
    }
  }
}
</script>

<style scoped>

</style>
